
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <base href="<%=basePath %>">
  <title>图书管理系统——公告管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="./layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="./layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto">
      <div class="layui-form-item">

        <div class="layui-inline">
          <label class="layui-form-label">公告主题：</label>
          <div class="layui-input-inline">
            <input type="text" name="name" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>
        <!--<div class="layui-inline">
          <label class="layui-form-label">公告内容</label>
          <div class="layui-input-inline">
            <input type="text" name="content" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>-->

        <div class="layui-inline">
          <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-notice-search">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn">搜索</i>
          </button>
        </div>

       <!-- <div class="layui-inline">
          <button onclick="reset()" class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-notice-search">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn">重置</i>
          </button>
        </div>-->
      </div>
    </div>

    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-list layui-btn-normal" data-type="add">发布公告</button>
        <button class="layui-btn layuiadmin-btn-list layui-btn-danger" data-type="del">删除</button>
      </div>

      <table id="LAY-app-notice-list" lay-filter="LAY-app-notice-list"></table>

      <script type="text/html" id="table-notice-list">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>查询详情</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
      </script>

    </div>
  </div>
</div>

<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="./layuiadmin/layui/layui.js"></script>
<script>
  layui.config({
    base: './layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'table'], function(){
    var table = layui.table
            ,form = layui.form;

    //监听搜索
    form.on('submit(LAY-app-notice-search)', function(data){
      var field = data.field;

      //执行重载
      table.reload('LAY-app-notice-list', {
        where: field
      });
    });

    //加载表格数据
    table.render({

      elem: '#LAY-app-notice-list'
      ,url:'/noticeList'
      ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
      ,page: true
      ,limit: 10
      ,limits:[10,20,30,50]  //数据分页条
      ,text: '对不起，加载出现异常！'
      ,cols:[[
         {type:'checkbox',fixes:'left'}
         //,{field:'id', title: 'ID'}
        ,{field:'name', title: '主题'}
        ,{field:'content', title: '内容'}
        ,{field:'user', title: '发布者'}
        ,{field:'releasetime', title: '创建时间'}
        ,{fixed: 'right',title:'操作', width:178, align:'center', toolbar: '#table-notice-list'}
      ]]

      //表格数据渲染



    })






    /*
      var $ = layui.$, active = {} 可用于执行表格数据的重载操作
        点击新增仓库 按钮时，添加表格，并重新加载表格数据

    */
    var $ = layui.$, active = {
      add:function () {
        layer.open({
          type: 2,
          title:'新增',
          content: '/noticeForm_add.html', //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
          maxmin:true,
          area:['550px','550px'],
          btn:['确定','取消'],
          yes:function (index,layero) {
            //当点击确定是，出发iframe的提交
            var submit =layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
            submit.click();
          }
        });
      }

      ,
      del: function () {
        var checkStatus = table.checkStatus('LAY-app-notice-list')
        var data = checkStatus.data;
        if (data.length == 0) {
          return;
        }
        var arr = [];
        data.forEach(function (value, index) {
          arr.push(value.id);
        });
        console.log(arr.join(","));
        $.ajax({
          url: '/noticeDelete',
          data:{"id":arr.join(",")},
          type: 'post',
          dataType: 'json',
          success: function (result) {
            if (result.code == 0) {
              var curr = parseInt($(".layui-laypage-curr em:last-child").text())  //获取当前页
              if (table.cache['LAY-app-notice-list'].length == 1 && curr > 1) {
                curr -= 1;
              }
              //添加成功
              layer.msg('删除成功', {
                offset: '15px'
                , icon: 1
                , time: 100
              }, function () {
                table.reload("LAY-app-notice-list", {page: curr});//重载表格
              });
            } else if (result.code == 1) {
              layer.open({
                title: '提示'
                , content: result.msg
              });
            }
          }
        })
      }

    };




    //给layuiadmin-btn-list中的按钮，绑定点击事件，点击按钮时，
    // 触发上面 var $ = layui.$, active 中的某个函数
    $('.layui-btn.layuiadmin-btn-list').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });


    //监听工具条， 当用户点击，编辑，删除按钮时，触发此函数
    table.on('tool(LAY-app-notice-list)', function(obj){
      var data = obj.data; //获得当前行数据
      var layEvent = obj.event; //获取具体事件

      //如果事件和  lay-event="edit"中值相等，则触发修改操作
      if(layEvent === 'edit'){ //
        layer.open({
          type: 2,
          title:'编辑',
          content: '/noticeForm_edit.html', //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
          maxmin:true,
          area:['550px','550px'],
          btn:['确定','取消'],
          success: function (layero,index) {
            var body = layer.getChildFrame('body', index);
            console.log(obj.data)
            //编辑页的数据回填
            body.find("#id").val(obj.data.id);
            body.find("#name").val(obj.data.name);
            body.find("#content").val(obj.data.content);
            body.find("#user").val(obj.data.user);



          },
          yes:function (index,layero) {
            //当点击确定是，出发iframe的提交
            var submit =layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
            submit.click();
          }
        });


      }

      //如果事件和  lay-event="del"中值相等，则触发删除操作
      if(layEvent === 'del'){ //
        var id = data.id;

        $.ajax({
          url:'/noticeDelete',
          data:{"id":id},
          type:'post',
          dataType:'json',
          success:function(result){

            if (result.code == 0){
              var curr = parseInt($(".layui-laypage-curr em:last-child").text())  //获取当前页
              if(table.cache['LAY-app-notice-list'].length == 1 && curr>1){
                curr -= 1;
              }
              //添加成功
              layer.msg('删除成功', {
                offset: '15px'
                ,icon: 1
                ,time:1000
              }, function(){

                table.reload("LAY-app-notice-list",{page:curr});//重载表格

              });
            }else if (result.code == 1){
              layer.open({
                title: '提示'
                ,content: result.msg
              });
            }
          }
        })

      }


    });

  });

  

  function reset() {
    $("select#deptId").val("");
    layui.use('form', function () {
      var form = layui.form;
      form.render("select");
    });
  }
</script>
</body>
</html>
